package xan.tm.digital.common.enums;

import xan.tm.digital.common.entity.User;
import xan.tm.digital.common.rules.Rule;

import java.text.MessageFormat;

/**
 * @author: renwq
 * @date: 2025/9/12 16:51
 * @sesc: 折扣规则枚举
 */
public enum DiscountRuleEnum implements Rule<User> {

    // 规则1：年龄>18，VIP为GOLD，10%
    AGE_VIP("GOLD", 18, 10.0, "{0} discount for GOLD VIP") {
        @Override
        public String apply(User user) {
            if (vipLevel.equals(user.getVipLevel()) && user.getAge() > minAge) {  // 这里访问实例字段 minAge，无问题
                return MessageFormat.format(description, discountPercent);
            }
            return null;
        }
    },
    // 规则2：年龄>65，无论VIP，5%
    SENIOR(null, 65, 5.0, "{0} discount") {
        @Override
        public String apply(User user) {
            if (user.getAge() > minAge) {  // 实例字段，特定于此常量
                return MessageFormat.format(description, discountPercent);
            }
            return null;
        }
    },
    // 规则3：VIP为SILVER，2%
    SILVER("SILVER", 0, 2.0, "{0} discount for SILVER VIP") {
        @Override
        public String apply(User user) {
            if (vipLevel.equals(user.getVipLevel())) {
                return MessageFormat.format(description, discountPercent);
            }
            return null;
        }
    };

    // 实例字段：每个常量独有
    final String vipLevel;  // 可为null表示忽略
    final int minAge;
    final double discountPercent;
    final String description;

    // 构造函数
    DiscountRuleEnum(String vipLevel, int minAge, double discountPercent, String description) {
        this.vipLevel = vipLevel;
        this.minAge = minAge;  // 赋值实例字段，无需static
        this.discountPercent = discountPercent;
        this.description = description;
    }
}
